package com.zjdiepu.www.manage.api.fk;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.AuditEditStatus;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.fk.IFkAuditConditionEditService;
import com.zjdiepu.www.ibiz.other.IOtherAuditInfoService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.fk.dto.FkAuditConditionEdit;
import com.zjdiepu.www.model.fk.ext.FkAuditConditionEditExt;
import com.zjdiepu.www.model.other.dto.OtherAuditInfo;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * fk_风控条件修改Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2018年01月31日 15时48分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "FkAuditConditionEditController", produces="application/json;charset=UTF-8")
@Api(tags = {"fk_风控条件修改"}, produces="application/json;charset=UTF-8")
public class FkAuditConditionEditController extends BaseController {
    @Resource
    private IFkAuditConditionEditService fkAuditConditionEditService;
    @Resource
   	private IYwBusinessService ywBusinessService;
   	@Resource
       private YwBusinessVoMapper ywBusinessVoMapper;
   	@Resource
   	private IOtherAuditInfoService otherAuditInfoService;

   	
    @ApiOperation(value = "风控审核数据插入数据", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "saveOtherAuditInfo", method = RequestMethod.POST)
    public ApiResponse saveOtherAuditInfo(@Validated(Add.class) @ModelAttribute FkAuditConditionEdit fkAuditConditionEdit) {
    	AbstractSysUser currentUser = getCurrentUser();
    	fkAuditConditionEdit.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	fkAuditConditionEdit.setModifierName(currentUser.getName());
        fkAuditConditionEditService.saveOtherAuditInfo(fkAuditConditionEdit,currentUser);
        return ApiResponse.success(true);
    }
    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增fk_风控条件修改 <br/>
     * @param fkAuditConditionEdit
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增fk_风控条件修改", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute FkAuditConditionEdit fkAuditConditionEdit) {
    	AbstractSysUser currentUser = getCurrentUser();
    	fkAuditConditionEdit.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	fkAuditConditionEdit.setModifierName(currentUser.getName());
        fkAuditConditionEditService.saveUpdateAndDelete(fkAuditConditionEdit, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除fk_风控条件修改<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除fk_风控条件修改", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	fkAuditConditionEditService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用fk_风控条件修改<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用fk_风控条件修改", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	fkAuditConditionEditService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable <br/>
     * @Description: 启用fk_风控条件修改<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用fk_风控条件修改", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	fkAuditConditionEditService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改fk_风控条件修改 <br/>
     * @param fkAuditConditionEdit
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改fk_风控条件修改", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute FkAuditConditionEdit fkAuditConditionEdit) {
    	AbstractSysUser currentUser = getCurrentUser();
    	fkAuditConditionEdit.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	fkAuditConditionEdit.setModifierName(currentUser.getName());
    	fkAuditConditionEditService.saveUpdateAndDelete(fkAuditConditionEdit, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询fk_风控条件修改<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询fk_风控条件修改", response = FkAuditConditionEdit.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       FkAuditConditionEdit fkAuditConditionEdit = fkAuditConditionEditService.selectById(id);
       if(null == fkAuditConditionEdit) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(fkAuditConditionEdit);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询fk_风控条件修改 <br/>
     * @param fkAuditConditionEditExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询fk_风控条件修改",response = FkAuditConditionEdit.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute FkAuditConditionEditExt fkAuditConditionEditExt){
        fkAuditConditionEditExt.notDeleted();
        PageVo page = getPage(fkAuditConditionEditExt);
        int records = fkAuditConditionEditService.countByExt(fkAuditConditionEditExt);
        page.setRecords(records);
        if (records > 0) {
            List<FkAuditConditionEdit> fkAuditConditionEdits = fkAuditConditionEditService.selectByExt(fkAuditConditionEditExt);
            page.setRows(fkAuditConditionEdits);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

    @ApiOperation(value = "风控条件修改",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "selectFkParamList", method = RequestMethod.POST)
    public ApiResponse selectFkParamList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
		PageVo page = getPage(ywBusinessQueryRequest);
		ywBusinessQueryRequest.setBaseWhere();
		ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits(currentUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
        ywBusinessQueryRequest.setBusiStatusLess(BusiStatus.SHEN_PI_WAN_CHENG.getCode());
        ywBusinessQueryRequest.setBusiStatusNqMore(BusiStatus.YIN_HANG_YI_FANG_KUAN.getCode());
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            FkAuditConditionEdit fkAuditConditionEdit=null;
            for (YwBusiness ywBusiness : ywBusinesss) {
            	fkAuditConditionEdit=new FkAuditConditionEdit();
            	fkAuditConditionEdit.setDpStatus(DpStatus.NORMAL.getCode());
            	fkAuditConditionEdit.setBusiId(ywBusiness.getId());
            	fkAuditConditionEdit=fkAuditConditionEditService.selectRefined(fkAuditConditionEdit);
            	if(fkAuditConditionEdit!=null){
            		ywBusiness.setAuditEditStatus(fkAuditConditionEdit.getAuditEditStatus());
            	}
			}
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
	}
    
    @ApiOperation(value = "风控条件审核列表",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "selectFkParamShList", method = RequestMethod.POST)
    public ApiResponse selectFkParamShList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
		PageVo page = getPage(ywBusinessQueryRequest);
		ywBusinessQueryRequest.setBaseWhere();
		ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits(currentUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
        ywBusinessQueryRequest.setAuditEditStatus(AuditEditStatus.DAISHENHE.getCode());
        ywBusinessQueryRequest.setOrderSql(" A.CREATED DESC ");
        int records = fkAuditConditionEditService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = fkAuditConditionEditService.selectMultiByExt(ywBusinessQueryRequest);
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    @ApiOperation(value = "风控条件修改历史列表",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "selectHisParamShList", method = RequestMethod.POST)
    public ApiResponse selectHisParamShList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser currentUser = (SysUser)getCurrentUser();
		PageVo page = getPage(ywBusinessQueryRequest);
		ywBusinessQueryRequest.setBaseWhere();
		ywBusinessQueryRequest.setBankLoan(currentUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits(currentUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
        int records = fkAuditConditionEditService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = fkAuditConditionEditService.selectMultiByExt(ywBusinessQueryRequest);
            OtherAuditInfo otherAuditInfo=null;
            for (YwBusiness ywBusiness : ywBusinesss) {
            	otherAuditInfo=new OtherAuditInfo();
            	otherAuditInfo.setBusiId(ywBusiness.getId());
            	otherAuditInfo.setModeId(ModeIDType.FKTJXGSH.getCode());
            	otherAuditInfo.setDpStatus(DpStatus.NORMAL.getCode());
            	otherAuditInfo.setOrderSql(" created desc ");
            	otherAuditInfo=otherAuditInfoService.selectRefined(otherAuditInfo);
            	if(otherAuditInfo!=null){
            		ywBusiness.setAuditPostil(otherAuditInfo.getAuditPostil());
            	}
			}
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
}
